package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.goods.entity.GoodsArticleStarEntity;
import com.yuanfeng.goods.mapper.GoodsArticleInfoMapper;
import com.yuanfeng.goods.mapper.GoodsArticleStarMapper;
import com.yuanfeng.goods.service.GoodsArticleStarService;
import com.yuanfeng.goods.vo.StarredArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName GoodsArticleStarServiceImpl
 * @Description  社区心得收藏表
 * @Author cyp
 * @Date 2023-07-18 15:03:07
 * @Version 1.0
 **/
@Service
public class GoodsArticleStarServiceImpl extends ServiceImpl<GoodsArticleStarMapper, GoodsArticleStarEntity>
        implements GoodsArticleStarService {

    @Autowired
    private GoodsArticleInfoMapper goodsArticleInfoMapper;

    @Override
    public ResponseResult starArticle(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        param.put("starUser",ui.getUserId());
        int starFlag = (int) param.get("starFlag");
        if (1 == starFlag) {
            //判断当前用户是否已经收藏
            Integer count = this.baseMapper.queryExistForStar(param);
            if (count > 0) {
                return ResponseResult.success("已收藏");
            }
            //插入收藏记录表
            this.baseMapper.insertStar(param);
        } else {
            //删除收藏记录表
            this.baseMapper.deleteStar(param);
        }
        //更新帖子收藏数
        goodsArticleInfoMapper.updateStarNum(param);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryStarList(Map<String, Object> param) {
        try {
            UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
            if(null == ui){
                return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
            } else {
                param.put("userId", ui.getUserId());
            }
            IPage<StarredArticleVO> page = this.baseMapper.getStarredArticleList(new Query<String>().getPage(param),param);
            PageUtils result = PageUtils.getPage(page);
            if(!page.getRecords().isEmpty()){
                List<String> starredArticleIds = page.getRecords().stream().map(StarredArticleVO::getArticleId).collect(Collectors.toList());
                List<StarredArticleVO> resultList = new ArrayList<>();
                for (String articleId : starredArticleIds) {
                    param.put("articleId", articleId);
                    StarredArticleVO article = goodsArticleInfoMapper.queryStarArticleInfo(param);
                    resultList.add(article);
                }
                Integer pageSize = Integer.parseInt(page.getSize()+"");
                result.setTotalCount((result.getTotalCount() % pageSize) == 0 ? result.getTotalCount() / pageSize : result.getTotalCount() / pageSize + 1);
                result.setList(resultList);
            }
            return ResponseResult.success(result);
        } catch (Exception e) {
        }
        return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(),BizCodeEnume.SYSTEM_ERROR.getMsg());
    }
}
